package io.seata.sample.service;

import io.seata.sample.dao.StorageDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * @author IT云清
 */
@Service("storageServiceImpl")
public class StorageServiceImpl implements StorageService {

    private static final Logger LOGGER = LoggerFactory.getLogger(StorageServiceImpl.class);

    @Autowired
    private StorageDao storageDao;

    /**
     * 扣减库存
     * @param productId 产品id
     * @param count 数量
     * @return
     */
    @Override
    public void decrease(Long productId, Integer count, Integer type) {
        LOGGER.info("------->type: {}", type);

        LOGGER.info("------->扣减库存开始");
        storageDao.decrease(productId,count);
        LOGGER.info("------->扣减库存结束");

        if (type==3){
            throw new NullPointerException("storage 减库存之后本地报错");
        }

    }

    @Override
    public void deleteById(Integer id, Integer type) {
        LOGGER.info("------->type: {}", type);
        LOGGER.info("------->删除storage开始");
        storageDao.deleteById(id);
        LOGGER.info("------->删除storage结束");
        if(type==2){
            throw new NullPointerException("storage 删除数据之后本地报错");
        }
    }
}
